package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.pojo.Container;
import com.fuhe.chen.vendingmachine.pojo.Machine;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.fuhe.chen.vendingmachine.service.IMachineService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 机器管理首页
 * 查询机器信息/添加机器/修改机器信息/删除机器/查看机器库存
 */
@Controller
@Api(tags = "机器管理")
@RequestMapping("/admin/machine")
public class MachineController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MachineController.class);

    @Autowired
    IMachineService machineService;

    @Autowired
    ICommodityService commodityService;

    /**
     * 根据id查询机器信息
     */
    @ApiOperation("根据id查询机器信息")
    @ResponseBody
    @GetMapping("/detail/{machineId}")
    public ApiResponse<Machine> queryMachineById(@PathVariable Integer machineId){
        Machine machine = machineService.findOne(machineId);

        ApiResponse<Machine> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(machine);

        return response;
    }

    /**
     * 根据机器id查询格子柜信息
     */
    @ApiOperation("根据机器id查询格子柜信息")
    @ResponseBody
    @GetMapping("/containers/{machineId}")
    public ApiResponseForList<Container> queryContainersByMachineId(@PathVariable Integer machineId){
        List<Container> containers = machineService.queryContainer(machineId);

        ApiResponseForList<Container> response = new ApiResponseForList<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(containers);

        return response;
    }

    /**
     * 查询所有机器
     */
    @ApiOperation("按页查询机器列表")
    @GetMapping("/queryMachineList")
    @ResponseBody
    public ApiResponse<PageInfo<Machine>> queryMachineList(@RequestParam("pageNum") int pageNum,
                                                @RequestParam("pageSize") int pageSize){

        PageInfo<Machine> all = machineService.findAllForAdmin(pageNum, pageSize);

        ApiResponse<PageInfo<Machine>> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(all);

        return response;
    }

    /**
     * 新增机器信息
     * @param place
     * @return
     */
    @ApiOperation("新增机器信息")
    @PostMapping("/addMachine")
    @ResponseBody
    @AddLog(operation = "新增机器信息")
    public  ApiResponse<String> addMachine(@RequestParam("place") String place,
                             @RequestParam("longitude") Double longitude,
                             @RequestParam("latitude") Double latitude,
                             @RequestParam("picture") MultipartFile picture,
                             @RequestParam("type") Integer type,
                             @RequestParam(value = "capacity",required = false) Integer capacity
                             ){

        ApiResponse<String> response = new ApiResponse<>();
        if(type==0){
            if (capacity==null||capacity<=0){
                response.setSuccess(false);
                response.setCode(MessageConstant.Machine.DEFEAT_CAPACITY_CODE);
                response.setMessage(MessageConstant.Machine.DEFEAT_CAPACITY_MESSAGE);
                return response;
            }
        }

        String imgPath = "#";

        Machine machine = new Machine();
        machine.setPlace(place);
        machine.setLongitude(longitude);
        machine.setLatitude(latitude);
        machine.setPicture(Utils.upload(picture, imgPath));
        machine.setMachineStatus(0);
        machine.setStock(0);
        machine.setCapacity(capacity);
        machine.setType(type);
        machineService.addMachine(machine);

        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.CREATE_SUCCESS_MESSAGE);

        return response;
    }

    /**
     * 修改机器信息
     * @param machineId
     * @return
     */
    @ApiOperation("修改机器信息")
    @PostMapping("/update")
    @ResponseBody
    @AddLog(operation = "修改机器信息")
    public ApiResponse<String> update(@RequestParam(name = "place",required = false) String place,
                         @RequestParam(name = "machineId",required = true) Integer machineId,
                         @RequestParam(name = "longitude",required = false) Double longitude,
                         @RequestParam(name = "latitude",required = false) Double latitude,
                         @RequestParam(name = "picture" ,required = false) MultipartFile picture){

        String imgPath = "#";
        Machine machine = new Machine();
        machine.setId(machineId);
        machine.setPlace(place);
        machine.setPicture(Utils.upload(picture, imgPath));
        machine.setLatitude(latitude);
        machine.setLongitude(longitude);
        machineService.updateMachine(machine);

        ApiResponse<String> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setMessage(MessageConstant.Success.UPDATE_SUCCESS_MESSAGE);

        return response;
    }


}
